<?php
use yii\helpers\Url;
?>
<div class="row-content am-cf">
    <div class="row">

        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-fl">编辑垃圾分类</div>
                    <div class="widget-function am-fr">
                        <a href="javascript:;" class="am-icon-cog"></a>
                    </div>
                </div>
                <div class="widget-body am-fr">

                    <form class="am-form tpl-form-border-form tpl-form-border-br">
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">分类名称</label>
                            <div class="am-u-sm-9">
                                <input type="text" class="tpl-form-input" id="name" placeholder="请输入分类名称" value="<?=$cate->name?>">

                            </div>
                        </div>


                        <div class="am-form-group">
                            <label for="user-phone" class="am-u-sm-3 am-form-label">上级分类 </label>
                            <div class="am-u-sm-9">
                                <select data-am-selected="{searchBox: 0}" id="cate" style="display: none;">
                                    <option value="0">顶级分类</option>
                                    <?php foreach ($cates as $c) {
                                        $selected="";
                                        if($c['id']==$cate->p_id){
                                            $selected="selected='true'";
                                        }
                                        echo "<option ".$selected."  value=\"".$c['id']."\">".$c['name']."</option>";
                                    }?>

                                </select>

                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">模板名称</label>
                            <div class="am-u-sm-9">
                                <select data-am-selected="{searchBox: 0}" id="tpl" style="display: none;">
                                    <option <?php if($cate['tpl']==1){ echo "selected='1'";}?> value="1">厨房垃圾模板</option>
                                    <option <?php if($cate['tpl']==2){ echo "selected='1'";}?> value="2">其他垃圾模板</option>

                                </select>

                            </div>
                        </div>





                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3">
                                <button id="submit" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success ">提交</button>
                            </div>
                        </div>
                    </form>
                    <script>
                        $("#submit").click(function(){
                            var name= $("#name").val();
                            var cate=$("#cate").val();
                            var cateName = $('#cate option:selected').text();
                            var tpl = $("#tpl").val();

                            if(name==''){
                                alert("分类名称不能为空！");
                                return ;
                            }

                            $.ajax({
                                url:"<?=Url::to(['category/edit-cate'])?>",
                                data:"name="+name+"&cate="+cate+"&cateName="+cateName+"&tpl="+tpl+"&id=<?=$cate['id']?>",
                                type:'post',
                                dataType:'json',
                                success:function(data){
                                    if(data.status){
                                        alert("修改成功");
                                        window.location.href="<?=Url::to(['category/list'])?>";
                                    }else{
                                        alert(data.msg);
                                    }
                                },
                                error:function(){
                                    alert("系统繁忙");
                                }
                            })
                        })

                    </script>
                </div>
            </div>
        </div>
    </div>
</div>